﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DoTweenDemo : MonoBehaviour {

    private RectTransform rt;

    public Vector2 from;
    public Vector2 to;
    public float duration;
    Tweener twTo = null;
    public Ease inEase;
    public Ease outEase;
    private void Start()
    {
        rt = GetComponent<RectTransform>();
        from = rt.anchoredPosition;
        
    }

    public void From()
    {
        if (twTo == null)
        {
            twTo = rt.DOAnchorPos(from, duration);
            twTo.SetAutoKill(false);
        }
        else
        {
            twTo.PlayBackwards();
        }

    }

    public void To()
    {
        if (twTo == null)
        {
            twTo = rt.DOAnchorPos(to, duration);
            twTo.SetAutoKill(false);
            twTo.OnPause(() => { Debug.Log(twTo.IsBackwards()); });
        }
        else
        {
            twTo.PlayForward();
        }
    }

    [ContextMenu("Set To")]
    public void SetTo()
    {
        to = transform.GetComponent<RectTransform>().anchoredPosition;
    }

    private void OnGUI()
    {
        if (GUILayout.Button("To"))
        {
            To();
        }

        if (GUILayout.Button("From"))
        {
            From();
        }

        if (GUILayout.Button("Jump"))
        {
            // 跳动
            rt.DOJumpAnchorPos(from, 5, 3, 1);
        }

        if (GUILayout.Button("Punch"))
        {
            // 来回冲压
            rt.DOPunchAnchorPos(to, 5);
        }

        if (GUILayout.Button("Fade"))
        {
            // 来回冲压
            GetComponent<Image>().DOFade(1, 1);
        }
    }
}
